package com.example.demo.practice;

import com.alibaba.fastjson.JSONObject;

/**
 * @author liweizhi
 * @date 2020/4/1 18:15
 */
public class JsonClone {
    public static void main(String[] args) {
        JSONObject one = new JSONObject();
        JSONObject two = new JSONObject();
        JSONObject three = new JSONObject();
        three.put("name", "three");
        two.put("child", three);
        one.put("child", two);

        JSONObject oneC = clone(one);
        oneC.getJSONObject("child").getJSONObject("child").put("name", "threeChanged!");
        System.out.println(one.getJSONObject("child").getJSONObject("child").getString("name"));
    }

    public static JSONObject clone(JSONObject from) {
        JSONObject to = (JSONObject) from.clone();
        to.forEach((s, o) -> {
            if (o instanceof JSONObject) {
                to.put(s, clone((JSONObject) o));
            }
        });
        return to;
    }
}
